package top.sigetech.springcloud.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import top.sigetech.springcloud.entity.Payment;
import top.sigetech.springcloud.handler.CustomerBlockHandler;
import top.sigetech.springcloud.vo.CommonResult;

@RestController
public class RateLimitController {

    @GetMapping("/byResource")
    @SentinelResource(value = "byResource",blockHandler = "handleException")
    public CommonResult<Object> byResource(){
        return new CommonResult<>(200,"按照资源名限流测试OK",new Payment(2020L,"serial001"));
    }
    public CommonResult<Object> handleException(BlockException ex){
        return new CommonResult<>(444,ex.getClass().getCanonicalName()+" \t 服务不可用");
    }

    @GetMapping("/rateLimit/byUrl")
    @SentinelResource(value = "byUrl")
    public CommonResult<Object> byUrl(){
        return new CommonResult<>(200,"按照url限流测试OK",new Payment(2020L,"serial002"));
    }

    @GetMapping("/rateLimit/handler")
    @SentinelResource(value = "byHandler",
            blockHandlerClass = CustomerBlockHandler.class,
            blockHandler = "handlerException2")
    public CommonResult<Object> byHandler(){
        return new CommonResult<>(200,"按照自定义限流测试OK",new Payment(2020L,"serial003"));
    }
}
